//
//  PushRecordViewController.m
//  AppleMarket
//
//  Created by yangy on 05/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "PushRecordViewController.h"
// model
#import "CoinAssetModel.h"
#import "UserDeposit.h"
// controller
#import "RecordDetailViewController.h"

@interface PushRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger curPage;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, assign) NSInteger pageCount;


@end

@implementation PushRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@充值记录",self.coinModel.Unit];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void) refersh
{
    [NetRequest request_GetGetUserDepositListWithCeId:self.coinModel.CurrencyID pageIndex:_curPage completeHandler:^(id  _Nonnull responseObj) {
        [self.tableView.mj_header endRefreshing];
        if([[responseObj objectForKey:@"Status"] boolValue]){
            NSDictionary *data = [responseObj objectForKey:@"Data"];
            NSArray *DataList = [data objectForKey:@"DataList"];
            self.dataList = [[[DataList.rac_sequence map:^id _Nullable(id  _Nullable value) {
                return [UserDeposit yy_modelWithDictionary:value];
            }] array] mutableCopy];
            self.pageCount = [[data objectForKey:@"TotalCount"] integerValue];
            self.curPage ++;
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void) loadMore
{
    [NetRequest request_GetGetUserDepositListWithCeId:self.coinModel.CurrencyID pageIndex:_curPage completeHandler:^(id  _Nonnull responseObj) {
        [self.tableView.mj_footer endRefreshing];
        if([[responseObj objectForKey:@"Status"] boolValue]){
            NSDictionary *data = [responseObj objectForKey:@"Data"];
            NSArray *DataList = [data objectForKey:@"DataList"];
            NSArray *temp = [[DataList.rac_sequence map:^id _Nullable(id  _Nullable value) {
                return [UserDeposit yy_modelWithDictionary:value];
            }] array];
            [self.dataList addObjectsFromArray:temp];
            self.pageCount = [[data objectForKey:@"TotalCount"] integerValue];
            self.curPage ++;
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.textLabel.font = kFontBold(18);
        cell.textLabel.textColor = kColorWithHex(0x0E304C);
        cell.detailTextLabel.textColor = kColorWithHex(0x808C99);
        cell.detailTextLabel.font = kFont(12);
        if(![cell.contentView viewWithTag:100]){
            UILabel *label = [UILabel cm_labelWithText:@"" font:kFont(15) textColor:kColorWithHex(0x4A647A) textAlign:NSTextAlignmentRight];
            label.tag = 100;
            label.frame = CGRectMake(self.view.bounds.size.width - 115, 21, 100, 15);
            [cell.contentView addSubview:label];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    UserDeposit *u = self.dataList[indexPath.row];
    
    cell.textLabel.text = u.TradeAmount;
    cell.detailTextLabel.text = u.CreateTime;
    
    UILabel *statusLabel = [cell.contentView viewWithTag:100];
    if(statusLabel){
        statusLabel.text = u.ConfirmedStatusName;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserDeposit *u = self.dataList[indexPath.row];
    RecordDetailViewController *detailVC = [[RecordDetailViewController alloc] init];
    detailVC.deposit = u;
    [self bh_PushViewController:detailVC hiddenTabbar:YES];
}

#pragma mark - lazy load
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSeparatorColor:kColorWithHex(0xEFEFEF)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
        [_tableView setRowHeight:80];
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setMj_header:[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.curPage = 1;
            [self refersh];
        }]];
        [_tableView setMj_footer:[MJRefreshAutoFooter footerWithRefreshingBlock:^{
            if(self.curPage < self.pageCount){
                [self loadMore];
            }
        }]];
        [_tableView.mj_header beginRefreshing];
    }
    return _tableView;
}

@end
